package cn.itsource.org.controller;

import cn.hutool.http.server.HttpServerRequest;
import cn.itsource.basic.annotation.Repeat;
import cn.itsource.basic.util.JsonResult;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.User;
import cn.itsource.org.service.IShopService;
import cn.itsource.org.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * department控制层
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private IUserService userService;
    @GetMapping("/checkphone/{phone}")
    @ApiOperation(value = "验证手机",notes = "验证手机")
    public JsonResult activeStore(@PathVariable("phone") String phone){
        userService.verifyPhone(phone);
        return JsonResult.createSuccess("手机号验证通过");
    }
    @PostMapping("/sendmobilecode")
    @ApiOperation(value = "验证码",notes = "验证码")
    //post用的是json对象
    @Repeat(2) //防刷机制
    public JsonResult verifyCode(@RequestBody User user, HttpServerRequest request){
        userService.verifyCode(user.getPhone(),request);
        return JsonResult.createSuccess("验证码发送成功");
    }
    @PostMapping()
    @ApiOperation(value = "注册",notes = "注册")
    //此注解能使用默认分组
    public JsonResult register(@RequestBody @Validated(User.PhoneRegister.class) User user){
        userService.register(user);
        return JsonResult.createSuccess();
    }
    @PostMapping("/login")
    @ApiOperation(value = "邮箱注册",notes = "邮箱注册")
    //此注解能使用默认分组
    public JsonResult emailRegister(@RequestBody @Validated(User.EmailRegister.class) User user){
        userService.emailRegister(user);
        return JsonResult.createSuccess();
    }
}





